#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>

int main(int argc, char const *argv[])
{
    int fd[2];
    int result = pipe(fd);
    if (result < 0)
    {
        printf("管道开启失败\n");
        return 1;
    }

    pid_t pid = fork();

    if (pid < 0)
    {
        printf("Fork失败\n");
        return 1;
    }

    if (pid == 0)
    {
        close(fd[1]);
        char buf[1024];
        // 子进程
        ssize_t read_len = read(fd[0], buf, 1024);
        write(STDOUT_FILENO, buf, read_len);
        close(fd[0]);
        return 0;
    }

    close(fd[0]);
    write(fd[1], "Hello!\n", 7);
    close(fd[1]);

    return 0;
}
